package com.example.ymm.modules1.controller.sys;

import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.sys.SysMenu;
import com.example.ymm.modules1.entity.my.sys.SysRole;
import com.example.ymm.modules1.entity.my.user.DbRewards;
import com.example.ymm.modules1.entity.my.user.DbUserRewards;
import com.example.ymm.modules1.query.DbRewardsQueryCriteria;
import com.example.ymm.modules1.query.DbUserRewardsQueryCriteria;
import com.example.ymm.modules1.query.SysMenuQueryCriteria;
import com.example.ymm.modules1.query.sys.SysRoleQueryCriteria;
import com.example.ymm.modules1.service.dto.ImportUserRewardsExcelDto;
import com.example.ymm.modules1.service.sys.SysMenuService;
import com.example.ymm.modules1.service.sys.SysRoleService;
import com.example.ymm.util.UtilValidate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/hl/sys/menu")
@Api(tags = "pc 菜单权限管理")
@Slf4j
@CrossOrigin
public class SysMenuController extends BaseController {

    @Autowired
    SysMenuService sysMenuService;

    @Autowired
    SysRoleService sysRoleService;


    @ApiOperation(value = "菜单管理列表")
    @RequestMapping(value = "/menuList", method = RequestMethod.POST)
    public CommonResult menuList(@Valid SysMenuQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<SysMenu> list = sysMenuService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }




    @ApiOperation(value = "奖惩规则保存")
    @RequestMapping(value = "/saveMenu", method = RequestMethod.POST)
    public CommonResult saveMenu(SysMenu menu) {
        try{
            if(UtilValidate.isEmpty(menu.getParentId())){
                menu.setParentId("0");
            }
            sysMenuService.saveOrUpdate(menu);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "菜单删除")
    @RequestMapping(value = "/menuDelete", method = RequestMethod.POST)
    public CommonResult menuDelete(@ApiParam(value = "数据主键") @RequestParam(required = true) String menuId) {
        try{
            sysMenuService.removeById(menuId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "获取树形菜单数据")
    @RequestMapping(value = "/getMenuTreeList", method = RequestMethod.POST)
    public CommonResult getMenuTreeList(SysMenuQueryCriteria criteria) {
        try{
            List<SysMenu> list = sysMenuService.getMenuTreeList(criteria);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "角色管理列表")
    @RequestMapping(value = "/roleList", method = RequestMethod.POST)
    public CommonResult roleList(@Valid SysRoleQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<SysRole> list = sysRoleService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }




    @ApiOperation(value = "角色保存")
    @RequestMapping(value = "/saveRole", method = RequestMethod.POST)
    public CommonResult saveRole(SysRole role) {
        try{
            sysRoleService.saveFn(role);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "角色详情")
    @RequestMapping(value = "/roleDetail", method = RequestMethod.POST)
    public CommonResult roleDetail(SysRole role) {
        try{
            SysRole role1 = sysRoleService.detailFn(role);

            return CommonResult.success(role1);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "角色删除")
    @RequestMapping(value = "/roleDelete", method = RequestMethod.POST)
    public CommonResult roleDelete(@ApiParam(value = "数据主键") @RequestParam(required = true) String id) {
        try{
            sysRoleService.removeById(id);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "角色管理下拉列表")
    @RequestMapping(value = "/roleSelectList", method = RequestMethod.POST)
    public CommonResult roleSelectList(@Valid SysRoleQueryCriteria criteria) {
        try{
            List<SysRole> list = sysRoleService.queryAll(criteria);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


}
